package com.jerry.chinesechess;

import com.jerry.chinesechess.model.Board;
import com.jerry.chinesechess.model.Piece;
import com.jerry.chinesechess.util.CanvasUtils;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class ChinesechessApplication extends Application {


    static public int girdW = 40;
    static public int canvasW = 9 * girdW;
    static public int girdH = 40;
    static public int canvasH = 10 * girdH;
    static public Boolean init = true;
    static public double chessW = 18.0;
    static public String fontName = "STSong";
    static public Piece selectChess = null;
    static public Double fontSize = 18.0;
    static public Board board = new Board();

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Canvas canvas = new Canvas(canvasW * 1.0, canvasH * 1.0);
        StackPane stackPane = new StackPane();
        Image img = new Image(ChinesechessApplication.class.getResourceAsStream("/image/c.png"));
        stackPane.setBackground(new Background(new BackgroundImage(img, null, null, null, null)));
        stackPane.setMinWidth(canvasW * 2);
        stackPane.setMinHeight(canvasH * 1.5);
        GraphicsContext gc = canvas.getGraphicsContext2D();
        //选棋子
        canvas.setOnMouseClicked(event -> {
            int i = (int) (event.getX() / girdW);
            int j = (int) (event.getY() / girdH);
            Piece chess = CanvasUtils.clickChess(event.getX(), event.getY());
            if (chess != null) {
                if (selectChess == null) {//选择棋子
                    CanvasUtils.selectChess(chess, gc);
                } else {//选中棋子，移动
                    CanvasUtils.moveChess(i, j, gc);
                }
            } else {
                CanvasUtils.moveChess(i, j, gc);
            }
        });
        CanvasUtils.drawShapes(gc);
        stackPane.getChildren().add(CanvasUtils.placeCanvas());
        stackPane.getChildren().add(CanvasUtils.rotateCanvas());
        stackPane.getChildren().add(canvas);
        root.getChildren().add(stackPane);
        primaryStage.setTitle("棋盘");
        primaryStage.setScene(new Scene(root, canvasW * 2, canvasH * 1.5));
        primaryStage.show();
    }
}
